"""
视频处理器基类

定义视频处理的通用接口和基础功能。
"""

from abc import abstractmethod
from typing import List, Dict, Any

from src.core.plugin import ProcessorPlugin


class VideoProcessor(ProcessorPlugin):
    """
    视频处理器抽象基类
    
    定义视频处理的标准接口。
    """
    
    @property
    def name(self) -> str:
        return self.__class__.__name__
    
    @property
    def version(self) -> str:
        return "1.0.0"
    
    @property
    def description(self) -> str:
        return "视频处理器基类"
    
    def get_supported_operations(self) -> List[str]:
        return ["slice", "transcode", "extract_info", "extract_thumbnails"]
    
    async def process(self, input_data: Any, operation: str = "slice", **kwargs) -> Any:
        """
        处理视频数据
        
        Args:
            input_data: 输入数据（通常是视频文件路径）
            operation: 操作类型
            **kwargs: 其他参数
        
        Returns:
            处理结果
        """
        if operation == "slice":
            return await self.slice_video(input_data, **kwargs)
        elif operation == "transcode":
            return await self.transcode_video(input_data, **kwargs)
        elif operation == "extract_info":
            return await self.get_video_info(input_data)
        elif operation == "extract_thumbnails":
            return await self.extract_thumbnails(input_data, **kwargs)
        else:
            raise ValueError(f"不支持的操作类型: {operation}")
    
    @abstractmethod
    async def slice_video(self, 
                         input_path: str,
                         output_dir: str,
                         max_duration: int = 30,
                         min_duration: int = 3,
                         **kwargs) -> List[str]:
        """
        切割视频
        
        Args:
            input_path: 输入视频路径
            output_dir: 输出目录
            max_duration: 最大切片时长（秒）
            min_duration: 最小切片时长（秒）
            **kwargs: 其他参数
        
        Returns:
            切片文件路径列表
        """
        pass
    
    @abstractmethod
    async def transcode_video(self,
                             input_path: str,
                             output_path: str,
                             **kwargs) -> None:
        """
        转码视频
        
        Args:
            input_path: 输入视频路径
            output_path: 输出视频路径
            **kwargs: 其他参数
        """
        pass
    
    @abstractmethod
    async def get_video_info(self, video_path: str) -> Dict[str, Any]:
        """
        获取视频信息
        
        Args:
            video_path: 视频文件路径
        
        Returns:
            视频信息字典
        """
        pass
    
    @abstractmethod
    async def extract_thumbnails(self,
                                video_path: str,
                                output_dir: str,
                                count: int = 3,
                                **kwargs) -> List[str]:
        """
        提取视频缩略图
        
        Args:
            video_path: 视频文件路径
            output_dir: 输出目录
            count: 缩略图数量
            **kwargs: 其他参数
        
        Returns:
            缩略图文件路径列表
        """
        pass


class AudioProcessor(ProcessorPlugin):
    """
    音频处理器抽象基类
    """
    
    @property
    def name(self) -> str:
        return self.__class__.__name__
    
    @property
    def version(self) -> str:
        return "1.0.0"
    
    @property
    def description(self) -> str:
        return "音频处理器基类"
    
    def get_supported_operations(self) -> List[str]:
        return ["extract", "convert", "analyze"]
    
    async def process(self, input_data: Any, operation: str = "extract", **kwargs) -> Any:
        """处理音频数据"""
        if operation == "extract":
            return await self.extract_audio(input_data, **kwargs)
        elif operation == "convert":
            return await self.convert_audio(input_data, **kwargs)
        elif operation == "analyze":
            return await self.analyze_audio(input_data, **kwargs)
        else:
            raise ValueError(f"不支持的操作类型: {operation}")
    
    @abstractmethod
    async def extract_audio(self, 
                           video_path: str,
                           output_path: str,
                           **kwargs) -> str:
        """从视频中提取音频"""
        pass
    
    @abstractmethod
    async def convert_audio(self,
                           input_path: str,
                           output_path: str,
                           **kwargs) -> str:
        """转换音频格式"""
        pass
    
    @abstractmethod
    async def analyze_audio(self, audio_path: str, **kwargs) -> Dict[str, Any]:
        """分析音频特征"""
        pass


class ProcessorFactory:
    """处理器工厂类"""
    
    _processors: Dict[str, type] = {}
    
    @classmethod
    def register(cls, name: str, processor_class: type) -> None:
        """注册处理器"""
        cls._processors[name] = processor_class
    
    @classmethod
    def create(cls, name: str, **kwargs) -> ProcessorPlugin:
        """创建处理器实例"""
        if name not in cls._processors:
            raise ValueError(f"未知的处理器类型: {name}")
        
        processor_class = cls._processors[name]
        return processor_class(**kwargs)
    
    @classmethod
    def list_processors(cls) -> List[str]:
        """列出所有注册的处理器"""
        return list(cls._processors.keys())


# 处理器装饰器
def processor(name: str):
    """处理器注册装饰器"""
    def decorator(cls):
        ProcessorFactory.register(name, cls)
        return cls
    return decorator
